package com.ruoyi.project.museum.exhibit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.config.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.museum.exhibit.mapper.ExhibitMapper;
import com.ruoyi.project.museum.exhibit.domain.Exhibit;
import com.ruoyi.project.museum.exhibit.service.IExhibitService;
import com.ruoyi.project.museum.exhibit.vo.ExhibitSimpleVO;
import com.ruoyi.common.utils.text.Convert;

/**
 * 文物管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-10
 */
@Service
public class ExhibitServiceImpl implements IExhibitService 
{
    @Autowired
    private ExhibitMapper exhibitMapper;

    @Autowired
    private IConfigService configService;

    /**
     * 查询文物管理
     * 
     * @param id 文物管理ID
     * @return 文物管理
     */
    @Override
    public Exhibit selectExhibitById(Integer id)
    {
        return exhibitMapper.selectExhibitById(id);
    }

    /**
     * 查询文物管理列表
     * 
     * @param exhibit 文物管理
     * @return 文物管理
     */
    @Override
    public List<Exhibit> selectExhibitList(Exhibit exhibit)
    {
        return exhibitMapper.selectExhibitList(exhibit);
    }

    /**
     * 新增文物管理
     * 
     * @param exhibit 文物管理
     * @return 结果
     */
    @Override
    public int insertExhibit(Exhibit exhibit)
    {
        exhibit.setCreateTime(DateUtils.getNowDate());
        return exhibitMapper.insertExhibit(exhibit);
    }

    /**
     * 修改文物管理
     * 
     * @param exhibit 文物管理
     * @return 结果
     */
    @Override
    public int updateExhibit(Exhibit exhibit)
    {
        exhibit.setUpdateTime(DateUtils.getNowDate());
        return exhibitMapper.updateExhibit(exhibit);
    }

    /**
     * 删除文物管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteExhibitByIds(String ids)
    {
        return exhibitMapper.deleteExhibitByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文物管理信息
     * 
     * @param id 文物管理ID
     * @return 结果
     */
    @Override
    public int deleteExhibitById(Integer id)
    {
        return exhibitMapper.deleteExhibitById(id);
    }

    public List<Exhibit> selectExhibitByTypeId(Integer typeId){
        return exhibitMapper.selectExhibitByTypeId(typeId);
    }

    public List<Exhibit> listIndex(){

        int indexCount = 10;
        String value = configService.selectConfigByKey("museum.index.count");
        if (!StringUtils.isEmpty(value)) {
           indexCount = Integer.parseInt(value) ;
        }

        Exhibit im = new Exhibit();
        im.setIsImportant("0");
        List<Exhibit> res = new ArrayList<Exhibit>();
        List<Exhibit> importantList = exhibitMapper.selectExhibitList(im);
        if(importantList != null && importantList.size() >= indexCount) {
            res.addAll(importantList.subList(0,indexCount));
        } else if(importantList != null && importantList.size() < indexCount) {
            List<Exhibit> others = exhibitMapper.selectRandomResults(indexCount - importantList.size());
            res.addAll(importantList);
            if(others.size() > 0) {
                res.addAll(others);
                int count = indexCount - res.size();
                if(res.size() < indexCount) {
                    for (int i = 0; i< count; i++ ) {
                        res.add(others.get(new Random().nextInt(others.size())));
                    }
                }
            }
        }
        return res;
    }

    @Override
    public Exhibit selectPervious(Exhibit exhibit){
        return exhibitMapper.selectPervious(exhibit);
    }
    @Override
    public Exhibit selectNext(Exhibit exhibit){
        return exhibitMapper.selectNext(exhibit);
    }
    @Override
    public List<ExhibitSimpleVO> listSimple(){
    	return exhibitMapper.selectSimpleList();
    }
}
